package com.jic.point.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfBdPlatfromRela;
import com.jic.point.model.entity.ConfBdPlatfromRelaApply;
import com.jic.point.model.vo.request.ConfBdPlatfromRelaRequest;
import com.jic.point.model.vo.response.ConfBdPlatfromRelaResponse;
import com.jic.point.service.ConfBdPlatfromRelaApplyService;
import com.jic.point.service.ConfBdPlatfromRelaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:32
 */
 @RestController
 @Slf4j
 @RequestMapping("/confBdPlatfromRela")
 @Api(tags = " [BD部门和平台关联表]控制类-PC")
public class ConfBdPlatfromRelaController {
	@Autowired
	private ConfBdPlatfromRelaService service;
	@Resource
    ConfBdPlatfromRelaApplyService applyService;
	
	
     /**
      *新增记录frozenListPage
      * @param request 新增[BD部门和平台关联表]请求参数
      * @return ConfBdPlatfromRelaResponse 新增[BD部门和平台关联表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[BD部门和平台关联表]记录")
    @PostMapping("/save")
    @ResponseBody

    
    public RestResult<Long> save(@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
     		ConfBdPlatfromRela confBdPlatfromRela = new ConfBdPlatfromRela();
    		BeanUtils.copyProperties(request, confBdPlatfromRela);
    	    long flag = service.save(confBdPlatfromRela);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[BD部门和平台关联表]请求参数
      * @return ConfBdPlatfromRelaResponse 新增或修改[BD部门和平台关联表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[BD部门和平台关联表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
      		ConfBdPlatfromRela confBdPlatfromRela = new ConfBdPlatfromRela();
		     BeanUtils.copyProperties(request, confBdPlatfromRela);
	         long flag =   service.saveOrUpdate(confBdPlatfromRela);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[BD部门和平台关联表]请求参数
      * @return ConfBdPlatfromRelaResponse 删除[BD部门和平台关联表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[BD部门和平台关联表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
      		ConfBdPlatfromRela confBdPlatfromRela = new ConfBdPlatfromRela();
	     	BeanUtils.copyProperties(request, confBdPlatfromRela);
            ConfBdPlatfromRela result = service.getByPrimaryKey(confBdPlatfromRela);
	     	//主表逻辑删除
            service.deleteByPrimaryKeyLogically(confBdPlatfromRela);
             //操作人
             result.setApplierId(request.getAdminOperate().getOperator());
             //插入到申请表
             ConfBdPlatfromRelaApply relaApply = new ConfBdPlatfromRelaApply();
             BeanUtils.copyProperties(result, relaApply);
             relaApply.setDeleteFlag(0);
             relaApply.setCurrentAction(2);
             relaApply.setAuditSts(0);
	         long flag = applyService.insertSelective(relaApply);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[BD部门和平台关联表]请求参数
      * @return ConfBdPlatfromRelaResponse 修改[BD部门和平台关联表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[BD部门和平台关联表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
      		ConfBdPlatfromRela confBdPlatfromRela = new ConfBdPlatfromRela();
	     	BeanUtils.copyProperties(request, confBdPlatfromRela);
	         long flag = service.updateByPrimaryKeySelective(confBdPlatfromRela);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[BD部门和平台关联表]详情请求参数
      * @return ConfBdPlatfromRelaResponse 查询[BD部门和平台关联表]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[BD部门和平台关联表]详情")
      @ResponseBody
     public RestResult<ConfBdPlatfromRelaResponse> getByPrimaryKey(@Validated  @RequestBody ConfBdPlatfromRelaRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfBdPlatfromRela confBdPlatfromRela = new ConfBdPlatfromRela();
	     	BeanUtils.copyProperties(request, confBdPlatfromRela);
	     	ConfBdPlatfromRela result = service.getByPrimaryKey(confBdPlatfromRela);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfBdPlatfromRelaResponse bo = new ConfBdPlatfromRelaResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfBdPlatfromRelaResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfBdPlatfromRela>> listPage (@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
      ConfBdPlatfromRela param = new ConfBdPlatfromRela();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
         return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/frozenListPage")
    @ApiOperation(value = " 分页冻结查询列表")
    @ResponseBody
    public RestResult<PageInfo<ConfBdPlatfromRela>> frozenListPage (@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
        ConfBdPlatfromRela param = new ConfBdPlatfromRela();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.frozenListPageBySelective(param, request.getPageNum(),request.getPageSize()));
    }
	
      /**
       *CONF_BD_PLATFROM_RELA 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [BD部门和平台关联表] ")
      @ResponseBody
      public RestResult<List<ConfBdPlatfromRelaResponse>>  listConfBdPlatfromRelaAllRecord(@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
      		ConfBdPlatfromRela param = new ConfBdPlatfromRela();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfBdPlatfromRela> list = service.listAllRecord(param);
			List<ConfBdPlatfromRelaResponse> responseList = list.stream().map(temp -> {
			ConfBdPlatfromRelaResponse bo = new ConfBdPlatfromRelaResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }
    /**
     *CONF_BD_PLATFROM_RELA 查询列表（不分页）
     * @param  request 前端请求参数
     */
    @PostMapping("/queryRelevance")
    @ApiOperation(value = "新增时，判断是否已添加 ")
    @ResponseBody
    public RestResult<List<ConfBdPlatfromRela>>  queryRelevance(@RequestBody ConfBdPlatfromRelaRequest request) throws Throwable {
        ConfBdPlatfromRela param = new ConfBdPlatfromRela();
        BeanUtils.copyProperties(request, param);
        List<ConfBdPlatfromRela> list = service.getqueryRelevance(param);
        return RestResult.success(list);
    }


}

	
